package com.qiyu.jvm.tlab;

import java.util.concurrent.CountDownLatch;

/**
 * -XX:+UseTLAB -Xcomp -XX:-BackgroundCompilation -XX:-DoEscapeAnalysis -server 233ms
 * -XX:-UseTLAB -Xcomp -XX:-BackgroundCompilation -XX:-DoEscapeAnalysis -server 3405ms
 *
 *   2:        462152       11091648  com.qiyu.jvm.tlab.User
 *
 * @Author linhao
 * @Date created in 3:24 下午 2021/1/17
 */
public class TLABTest {

    public static void alloc() {
        byte[] b = new byte[2];
        b[0] = 1;
        for(int i =0; i<10;i++){
            User u1 = new User();
            String t = new String();
            t = "idea";
            u1.setUsername("idea");
            u1.setId(1001);
        }
    }

    public static void main(String[] args) {
        long begin = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        CountDownLatch totalFinish = new CountDownLatch(10);
        for (int i=0;i<10;i++){
            Thread t = new Thread(new Runnable(){

                @Override
                public void run() {
                    try {
                        System.out.println("堵塞ing");
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    for (int i = 0; i < 1000000; i++) {
                        alloc();
                    }
                    totalFinish.countDown();
                }
            });
            t.start();
        }
        countDownLatch.countDown();
        try {
            totalFinish.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - begin));
        while (true){

        }
    }
}
